import {
  Body,
  Controller,
  Del,
  File,
  Get,
  Inject,
  Param,
  Post,
  Put
} from '@midwayjs/core'
import { Context } from '@midwayjs/koa'
import {
  ApiBearerAuth,
  ApiOperation,
  ApiResponse,
  ApiTags
} from '@midwayjs/swagger'
import { UploadFileInfo } from '@midwayjs/upload'
import { Validate } from '@midwayjs/validate'
import { ExcelService } from '@ttou/midway-excel'

import { Permission } from '@/decorator'
import { DeleteDTO } from '@/dto'

import {
  CreateUserDTO,
  FindUserPageDTO,
  ResetPasswordDTO,
  UpdateUserDTO
} from './user.dto'
import { UserExportSerialize, UserTemplateSerialize } from './user.serialize'
import { UserService } from './user.service'
import {
  GetUserAddFormDataVO,
  GetUserEditFormDataVO,
  GetUserPageVO
} from './user.vo'

@ApiBearerAuth()
@ApiTags('系统/用户接口')
@Controller('/system/user')
export class UserController {
  @Inject()
  ctx: Context

  @Inject()
  userService: UserService

  @Inject()
  excelService: ExcelService

  @ApiOperation({ summary: '获取用户分页' })
  @ApiResponse({ status: 200, type: GetUserPageVO })
  @Permission('system:user:list')
  @Post('/page')
  @Validate()
  async getUserPage(@Body() dto: FindUserPageDTO) {
    return await this.userService.handleFindPage(dto)
  }

  @ApiOperation({ summary: '获取用户新建表单数据' })
  @ApiResponse({ status: 200, type: GetUserAddFormDataVO })
  @Get('/formData/')
  async getUserAddFormData() {
    return await this.userService.handleGetFormData()
  }

  @ApiOperation({ summary: '获取用户更新表单数据' })
  @ApiResponse({ status: 200, type: GetUserEditFormDataVO })
  @Get('/formData/:id')
  async getUserEditFormData(@Param('id') id: string) {
    return await this.userService.handleGetFormData(id)
  }

  @ApiOperation({ summary: '新建用户' })
  @Permission('system:user:add')
  @Post('/create')
  @Validate()
  async createUser(@Body() dto: CreateUserDTO) {
    await this.userService.handleCreate(dto)
  }

  @ApiOperation({ summary: '更新用户' })
  @Permission('system:user:edit')
  @Put('/update/:id')
  @Validate()
  async updateUser(@Param('id') id: string, @Body() dto: UpdateUserDTO) {
    await this.userService.handleUpdate(id, dto)
  }

  @ApiOperation({ summary: '删除用户' })
  @Permission('system:user:remove')
  @Del('/delete')
  @Validate()
  async deleteUser(@Body() dto: DeleteDTO) {
    await this.userService.handleDelete(dto)
  }

  @ApiOperation({ summary: '导入用户' })
  @Permission('system:user:import')
  @Post('/template/import')
  async importUserTemplate(@File() file: UploadFileInfo<string>) {
    const result = await this.excelService.handleImport({
      sheetName: '用户',
      filePath: file.data,
      Cls: UserTemplateSerialize
    })

    return await this.userService.handleImport(result)
  }

  @ApiOperation({ summary: '导出用户模版' })
  @Post('/template/export')
  async exportUserTemplate() {
    const result = await this.excelService.handleTemplate({
      sheetName: '用户',
      filename: '用户模板',
      Cls: UserTemplateSerialize
    })

    this.ctx.set(result.headers)
    this.ctx.body = result.body
  }

  @ApiOperation({ summary: '导出用户分页' })
  @Permission('system:user:export')
  @Post('/page/export')
  @Validate()
  async exportUserPage(@Body() dto: FindUserPageDTO) {
    const { list } = await this.userService.handleFindPage(dto)

    const result = await this.excelService.handleExport({
      sheetName: '用户',
      filename: '用户报表',
      Cls: UserExportSerialize,
      data: list
    })

    this.ctx.set(result.headers)
    this.ctx.body = result.body
  }

  @ApiOperation({ summary: '重置密码' })
  @Put('/resetPassword/:id')
  @Validate()
  async resetPassword(@Param('id') id: string, @Body() dto: ResetPasswordDTO) {
    await this.userService.handleResetPassword(id, dto)
  }

  @ApiOperation({ summary: '切换用户状态' })
  @Put('/toggleStatus/:id')
  async toggleUserStatus(@Param('id') id: string) {
    await this.userService.handleToggleStatus(id)
  }
}
